<?php

namespace Library;

use Library\Wx;
use Think\Log;

/**
 * 微信事件通知处理类
 *
 * @author pufan
 */
class WxEvent {
    /*
     *
     */
    public function handle_subscribe($event) {
        $External = new \Api\Controller\ExternalController();
		$External->event($event);
		$External->qrcode($event);
    }

    /*
     *
     */
    public function handle_unsubscribe($event) {
        $External = new \Api\Controller\ExternalController();
		$External->qrcode($event);
    }

    /*
     *
     */
    public function handle_scan($event) {
        $External = new \Api\Controller\ExternalController();
		$External->event($event);
		$External->qrcode($event);
    }

    /*
     * 处理微信摇一摇事件通知
     */
    public function handle_shakearoundusershake($event) {
        Log::record(json_encode($event));

        $shark_old_ts = S('shake_event_ts:'.$event['FromUserName']);

        if ($shark_old_ts['CreateTime'] == $event['CreateTime']) {
            $shark_ts = $shark_old_ts;
        } else {
            $shark_ts['CreateTime'] = $event['CreateTime'];
        }

        $ChosenPage['ChosenPageId'] = $event['ChosenPageId'];
        $ChosenPage['ChosenBeacon'] = $event['ChosenBeacon'];
        $shark_ts['ChosenPageList'][] = $ChosenPage;

        S('shake_first_mark:'.$event['FromUserName'], 1, 60*5);
        S('shake_event_ts:'.$event['FromUserName'], $shark_ts, C('WX_SHAKE_TIMEOUT'));
    }

    /*
     * 处理摇一摇用户位置事件通知
     */
    public function handle_location($event) {
        $data['openid'] = $event['FromUserName'];
        $data['latitude'] = $event['Latitude'];
        $data['longitude'] = $event['Longitude'];
        $data['precision'] = $event['Precision'];
        try {
            D('user_location')->add($data);
        } catch (\Exception $e) {
            \Think\Log::record('handle_location catch Exception'.$e->getMessage());
        }
    }
}